class BasicArray {
    //1. 容量
    constructor(_capacity) {
        this.array = [];
        this._size = 0;
        this._capacity = _capacity ? _capacity : 10;
    }

    //2. insert(value, index)
    insert(value, index) {
        if (index > this._size || index < 0 || this._size === this._capacity) {
            return false;
        }
        for (let i = this._size; i > index; i--) {
            this.array[i] = this.array[i - 1];
        }
        this.array[index] = value;
        this._size += 1;
        return true;
    }

    //3. remove(index)
    remove(index) {
        // 批注：
        // if(index < 0 || index >= this._size) 更好
        // 因为 Array 的有效范围是 [0, this._size)，左闭右开，所以无效范围为 [this._size, Infinity)
        if (index < 0 || index > this._size - 1) {
            return false;
        }

        for (let i = index; i < this._size - 1; i++) {
            this.array[i] = this.array[i + 1];
        }
        this._size -= 1;
        return true;
    }


    //4. get(index)
    get(index) {
        // 批注：
        // if(index < 0 || index >= this._size) 更好
        // 因为 Array 的有效范围是 [0, this._size)，左闭右开，所以无效范围为 [this._size, Infinity)
        if (index > this._size - 1 || index < 0) {
            return undefined;
        }
        return this.array[index];
    }

    //5.getArray()
    getArray() {
        let resultArr = [];
        for (let i = 0; i < this._size; i++) {
            resultArr[i] = this.array[i]
        }
        return resultArr;
    }

    //6.size()
    size() {
        return this._size;
    }

    //7._capacity()
    capacity() {
        return this._capacity;
    }
}

let newArray = new BasicArray(10);
newArray.insert(1, 0)
console.log(newArray.get(-1));
export default BasicArray;